package cn.achao.csr.system.dao;

import cn.achao.csr.api.model.dto.RolePageDTO;
import cn.achao.csr.api.model.entity.SysRole;
import cn.achao.csr.base.utils.StringUtils;
import cn.achao.csr.datascope.annotation.DataScope;
import cn.achao.csr.system.mapper.SysRoleMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;

import java.util.*;

@Slf4j
@Service
@RequiredArgsConstructor
public class SysRoleDao extends ServiceImpl<SysRoleMapper, SysRole> {

    private final SysRoleMapper roleMapper;

    public Page<SysRole> selectRoleList(RolePageDTO req, Page page) {
        return roleMapper.selectRoleList(req, page);
    }

    /**
     * 根据用户ID查询角色权限
     * @param userId
     * @return
     */
    public Collection<String> selectRolePermissionByUserId(Long userId) {
        List<SysRole> perms = roleMapper.selectRolePermissionByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (SysRole perm : perms) {
            if (StringUtils.isNotNull(perm)) {
                permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
            }
        }
        return permsSet;
    }
}

